package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.hmdp.service.IUserService;
import com.hmdp.utils.UserHolder;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    private final StringRedisTemplate stringRedisTemplate;
    @Resource
    private IUserService userService;

    public FollowServiceImpl(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    //    关注
    @Override
    public Result follow(Long followUserId, boolean isFollow) {
//        1.获取登录用户
        Long userId = UserHolder.getUser().getId();
        String key = "follows:" + userId;
//        2,判断是关注还是取关
        if (isFollow) {
//            查看是否已经关注了
            Integer count = query().eq("user_id", userId).eq("follow_user_id", followUserId).count();
            if (count != 0) {
                return Result.fail("已关注，请勿重复关注");
            }
            //2.1 关注
            Follow follow = new Follow();
            follow.setFollowUserId(followUserId);
            follow.setUserId(userId);
            boolean isSave = save(follow);
            if (isSave) {
//                把关注用户的id，放入redis的set集合中，
                stringRedisTemplate.opsForSet().add(key, followUserId.toString());
                return Result.ok("关注成功");
            } else {
                return Result.fail("关注失败");
            }
        } else {
            //2.2 取关
            boolean isRemove = remove(new QueryWrapper<Follow>().eq("follow_user_id", followUserId).eq("user_id", userId));
            if (isRemove) {
                //取消关注就把关注用户id从缓存中删除
                stringRedisTemplate.opsForSet().remove(key, followUserId.toString());
                return Result.ok("取关成功");
            } else {
                return Result.fail("取关失败");
            }
        }

    }

    @Override
    public Result isFollow(Long followUserId) {
        return null;
    }

    @Override
    public Result followCommons(Long id) {
        Long myUserId = UserHolder.getUser().getId();
        String key1 = "follows:" + myUserId;
        String key2 = "follows:" + id;
//        求redis中两个集合的交集
        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(key1, key2);
        if (intersect == null || intersect.isEmpty()) {
            return Result.ok(Collections.emptyList());//没有共同关注
        }
//        有共同关注，就要通过其中的id解析出用户信息
        List<Long> ids = intersect.stream().map(Long::valueOf).collect(Collectors.toList());
        //查询用户
        List<UserDTO> users = userService.listByIds(ids).stream().map(user -> BeanUtil.copyProperties(user, UserDTO.class)).collect(Collectors.toList());

        return Result.ok(users);
    }
}
